package vip.zhaotao.utils;

import freemarker.template.TemplateException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.StreamUtils;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * 文件工具类
 */
public class FileUtils {

    /**
     * utf-8编码
     *
     * @return
     */
    public static Charset utf8() {
        return Charset.forName("UTF-8");
    }

    /**
     * 获取源文件路径
     *
     * @param resourceName
     * @return
     */
    public static String getResourcePath(String resourceName) {
        return FileUtils.class.getClassLoader().getResource(resourceName).getPath();
    }

    /**
     * 生成临时目录
     *
     * @return
     */
    public static File generateTempDirectory() {
        String tempDir = System.getProperty("java.io.tmpdir");
        String uuid = UUID.randomUUID().toString().replace("-", StringUtils.EMPTY).toUpperCase();
        File tempDirectory = new File(tempDir, uuid);
        if (!tempDirectory.exists()) {
            tempDirectory.mkdirs();
        }
        return tempDirectory;
    }

    /**
     * 删除临时目录以及目录下的文件
     *
     * @param tempDirectory 临时目录
     */
    public static void deleteTempDirectory(File tempDirectory) {
        if (tempDirectory != null) {
            File[] files = tempDirectory.listFiles();
            if (files != null) {
                for (File file : files) {
                    file.deleteOnExit();
                }
            }
            tempDirectory.deleteOnExit();
        }
    }

    /**
     *
     *
     * @param outputStream
     * @param dataModel
     * @throws IOException
     * @throws TemplateException
     */

    /**
     * 生成文件
     *
     * @param outputStream              输出流
     * @param dataModel                 数据
     * @param resourceFileDirectoryName 资源文件目录名称
     * @param templateFileName          模板文件名称
     * @param xmlFileDirectoryName      xml文件目录名称
     * @param xmlFileName               xml文件名称
     * @param xmlTemplateFileName       xml模板文件名称
     * @throws IOException
     * @throws TemplateException
     */
    public static void generateFile(OutputStream outputStream,
                                    Object dataModel,
                                    String resourceFileDirectoryName,
                                    String templateFileName,
                                    String xmlFileDirectoryName,
                                    String xmlFileName,
                                    String xmlTemplateFileName) throws IOException, TemplateException {
        Charset charset = FileUtils.utf8();
        String resourcePath = FileUtils.getResourcePath(resourceFileDirectoryName);
        ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream, charset);

        ZipFile zipFile = new ZipFile(new File(resourcePath, templateFileName));
        Enumeration<? extends ZipEntry> entries = zipFile.entries();

        ZipEntry zipEntry;
        String entryName;
        while (entries.hasMoreElements()) {
            zipEntry = entries.nextElement();
            entryName = zipEntry.getName();
            zipOutputStream.putNextEntry(new ZipEntry(entryName));
            if (entryName.equals(StringUtils.join(xmlFileDirectoryName, "/", xmlFileName))) {
                FreeMarkerUtils.templateProcess(
                        dataModel,
                        new OutputStreamWriter(zipOutputStream),
                        resourcePath,
                        xmlTemplateFileName,
                        charset.name()
                );
            } else {
                StreamUtils.copy(zipFile.getInputStream(zipEntry), zipOutputStream);
            }
        }
        zipOutputStream.flush();
        zipOutputStream.close();
    }
}
